package com.drinks.yiniang.ui.activity.user.milepost;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.listview.MilepostProgressAdapter;
import com.drinks.yiniang.adapter.user.milepost.MilepostBannerAdapter;
import com.drinks.yiniang.adapter.user.milepost.MilepostDetailAdapter;
import com.drinks.yiniang.entity.user.milepost.MilepostCategoryJson;
import com.drinks.yiniang.entity.user.milepost.MilepostCategoryUnlockJson;
import com.drinks.yiniang.entity.user.milepost.WineAnalyseJson;
import com.drinks.yiniang.entity.user.points.PointsExchangeJson;
import com.drinks.yiniang.io.api.MilepostApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.ui.widget.recycler.RecyclerCoverFlow;
import com.drinks.yiniang.utils.GlideUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.listview.NoScrollListView;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 里程碑分类详情页面
 *
 * @author han
 * @date 2019/09/10
 */
public class MilepostDetailActivity extends BaseTranBarActivity implements OnNavgationBarClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    //nav
    private NavigationBarLayout nav_bar_milepost_detail;
    //列表
    private BaseRecyclerView rcv_milepost_detail;

    //header view
    //头像
    private CircleImageView iv_milepost_category_avatar;
    //昵称
    private TextView tv_milepost_category_nickname;
    //消费总瓶数
    private TextView tv_milepost_category_total_bottles;
    //banner
    private RecyclerCoverFlow banner_milepost_detail;
    //分类进度
    private NoScrollListView lv_milepost_detail_progress;
    //解锁分类总数
    private TextView tv_milepost_category_unlock_num;

    private MilepostDetailAdapter mMilepostDetailAdapter;
    private MilepostProgressAdapter mProgressAdapter;
    private MilepostBannerAdapter mBannerAdapter;

    private List<WineAnalyseJson.WineAnalyseStyles> mProgressList;
    //banner
    private List<String> mBanners;

    //分类id
    private String mCategoryId;

    private int mPages;

    @Override
    protected void initView() {
        nav_bar_milepost_detail = findViewById(R.id.nav_bar_milepost_detail);

        rcv_milepost_detail = findViewById(R.id.rcv_milepost_detail);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mCategoryId = getIntent() != null ? getIntent().getStringExtra(Constants.KeyCategoryId) : null;

        initAdapter();
        initProgressAdapter();
        initBannerAdapter();

        getMilepostCategoryData();
        getUnlockCategoryList();
    }

    private void initAdapter() {
        rcv_milepost_detail.setLayoutManager(new LinearLayoutManager(mContext));

        mMilepostDetailAdapter = new MilepostDetailAdapter(mContext, new ArrayList<MilepostCategoryUnlockJson.MilepostCategoryUnlockList>());
        mMilepostDetailAdapter.addHeaderView(getHeaderView());
        mMilepostDetailAdapter.setHeaderAndEmpty(true);
        mMilepostDetailAdapter.setCommonEmpty(getLayoutInflater(), rcv_milepost_detail);
        mMilepostDetailAdapter.bindToRecyclerView(rcv_milepost_detail);
    }

    private void initProgressAdapter() {
        mProgressList = new ArrayList<>();

        mProgressAdapter = new MilepostProgressAdapter(mContext, mProgressList);

        lv_milepost_detail_progress.setAdapter(mProgressAdapter);
    }

    private void initBannerAdapter() {
        mBanners = new ArrayList<>();

        mBannerAdapter = new MilepostBannerAdapter(mContext, mBanners);

        banner_milepost_detail.setAdapter(mBannerAdapter);
    }

    //获取header
    private View getHeaderView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.header_milepost_detail, (ViewGroup) rcv_milepost_detail.getParent(), false);

        iv_milepost_category_avatar = mHeaderView.findViewById(R.id.iv_milepost_category_avatar);

        tv_milepost_category_nickname = mHeaderView.findViewById(R.id.tv_milepost_category_nickname);
        tv_milepost_category_total_bottles = mHeaderView.findViewById(R.id.tv_milepost_category_total_bottles);
        tv_milepost_category_unlock_num = mHeaderView.findViewById(R.id.tv_milepost_category_unlock_num);

        banner_milepost_detail = mHeaderView.findViewById(R.id.banner_milepost_detail);

        lv_milepost_detail_progress = mHeaderView.findViewById(R.id.lv_milepost_detail_progress);

        setTotalBottles(null);

        return mHeaderView;
    }

    @Override
    protected void initListener() {
        nav_bar_milepost_detail.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_milepost_detail;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        startActivity(new Intent(mContext, MilepostShareActivity.class));
    }

    @Override
    public void onLoadMoreRequested() {
        MilepostApiIO.getInstance().getMilepostCategoryList(mCategoryId, mPages, new APIRequestCallback<MilepostCategoryUnlockJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(MilepostCategoryUnlockJson res) {
                if(mMilepostDetailAdapter == null) {
                    return;
                }

                mPages++;
                mMilepostDetailAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mMilepostDetailAdapter.loadMoreComplete();
                } else {
                    mMilepostDetailAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mMilepostDetailAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //获取解锁的分类列表
    private void getUnlockCategoryList() {
        if(StringUtils.isEmpty(mCategoryId)) {
            ToastUtils.showShort(R.string.text_the_category_not_exist);
            return;
        }

        mPages = 1;

        MilepostApiIO.getInstance().getMilepostCategoryList(mCategoryId, mPages, new APIRequestCallback<MilepostCategoryUnlockJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(MilepostCategoryUnlockJson res) {
                if(mMilepostDetailAdapter == null) {
                    return;
                }

                mPages++;
                mMilepostDetailAdapter.getData().clear();
                mMilepostDetailAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mMilepostDetailAdapter.loadMoreComplete();
                } else {
                    mMilepostDetailAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mMilepostDetailAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //获取里程碑分类数据
    private void getMilepostCategoryData() {
        if(StringUtils.isEmpty(mCategoryId)) {
            ToastUtils.showShort(R.string.text_the_category_not_exist);
            return;
        }

        showLoadingDialog();

        MilepostApiIO.getInstance().getMilepostCategoryData(mCategoryId, new APIRequestCallback<MilepostCategoryJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(MilepostCategoryJson res) {
                setData(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setData(MilepostCategoryJson.MilepostCategoryData data) {
        //头像
        GlideUtils.loadAvatar(mContext, iv_milepost_category_avatar, data.getAvatar());
        //昵称
        tv_milepost_category_nickname.setText(data.getUsername());
        //总瓶数
        setTotalBottles(data.getTotal());
        //解锁总分类
        tv_milepost_category_unlock_num.setText(String.format(getString(R.string.text_unlock_category_format), StringUtils.getNullEmptyConvertZero(data.getTotal())));

        //banner
        banner_milepost_detail.setVisibility(CommonUtils.getListSize(data.getImages()) > 0 ? View.VISIBLE : View.GONE);

        if(data.getImages() != null) {
            mBanners.clear();
            mBanners.addAll(data.getImages());
            mBannerAdapter.notifyDataSetChanged();
        }

        if(data.getStyles() != null) {
            mProgressList.clear();
            mProgressList.addAll(data.getStyles());
            mProgressAdapter.notifyDataSetChanged();
        }
    }

    //总瓶数
    private void setTotalBottles(String total) {
        total = StringUtils.getNullEmptyConvertZero(total);
        SpannableStringBuilder builder = new SpannableStringBuilder(String.format(getString(R.string.text_total_bottles_format), total));

        int end = builder.length();
        int start = end - total.length();

        builder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.setSpan(new AbsoluteSizeSpan(ScreenSizeUtils.sp2Px(mContext, 14)), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_milepost_category_total_bottles.setText(builder);
    }
}
